constant
Type
command
Summary
Declares one or more constants and assigns values to them.
Syntax
constant <constantName> [= <value>] [, <constantName> [= <value>] ...]
Description
Use the constant command to assign an unchanging value to a keyword.
A constant's value can be anything, including arrays, as long as it can be defined by an expression which can be computed when the script is compiled. The majority of operators and functions can be used, as long as their operands and arguments are literals or other (previously defined) constants.
If the expression uses functions or operators which use local properties, then the properties will be taken to be the default values, regardless of where the constant is defined.
Choose easy-to-understand names for constants to improve the readability of your code. Use constants as substitutes for long or convoluted strings. Use the prefix letter k for your constant names to follow convention and make it easy to recognise. See the Tips For Writing Good Code in the Livecode Script Guide.
If you place the constant statement in a handler, you can use the constant anywhere in the handler. If you place the constant statement in a script outside any handler, you can use the constant anywhere in the handlers of that script.
Once you have defined a constant, you cannot redefine it in the same handler; doing so causes an execution error.
You can use the constant command to redefine LiveCode's built-in constants within a script or handler, but doing so makes your code harder to read and maintain. To find out whether a word is already defined as a built-in constant, use the constantNames function.
To see a list of built-in constants, open the Documentation window, click LiveCode Dictionary, and choose "Constants" from the menu at the top of the window.
Parameters
Name | Type | Description |
---|---|---|
constantName | The constantName is any identifier. | |
value | The value is any constant expression. |
Examples
constant kDefaultName = "Jones"
constant kEntryA = "EF9993333WX786", kEntryB = "GJ773281YX342"
constant kPiBy2 = pi / 2
constant kFoo = "foo"
constant kBar = "bar"
constant kFooBar = kFoo & kBar
constant kMap = { "a": 1, "b": 1 + 2 }
Related
function: constantNames
glossary: LiveCode, value, handler, string, statement, constant, keyword, command, declare, local property
property: script
constant: watch, false, slash, comma, plus, null, pi, iBeam, colon, three, formfeed, help, five, zero, seven, eight, CRLF, quote, empty, space, arrow, six, ten, four, two
control structure: function
Compatibility and Support
Introduced
LiveCode 1.0
OS
mac
windows
linux
ios
android
Platforms
desktop
server
mobile